<?php
/**
 * Created by PhpStorm.
 * User: jsq
 * Date: 2017/5/3 0003
 * Time: 14:05
 */
namespace app\admin\controller;

use app\common\model\Order;
use app\common\model\Order_log;
use app\common\traits\Gate;
use think\Db;
use think\Request;
use think\Facade\Session;
use app\common\traits\Page;


class Yewu extends Admin
{
    use Page;
    public function olist(Request $request)
    {
        $order = new Order();
        $map = $request->param();
        if(!empty($map['editid'])){//用作编辑锚点定位
            $this->assign('editid',$map['editid']);
        }else{
            $this->assign('editid',0);
        }
        if(!empty($map['editid'])){
            $mapa = session('map');
            $map  = array_merge($mapa,['p'=>$map['p']]);
        }
        $map = session('map',$map);
        $map = session('map');
        if( !empty($map['maphone'])  ){//电话搜索
            $list = $order->where(['maphone|otherphone'=>$map['maphone'],'status'=>1])->select();
        }elseif ( !empty($map['otherphone']) ){//电话搜索
            $list = $order->where(['maphone|otherphone'=>$map['otherphone'],'status'=>1])->select();
        }else{
            $data = $order->maps($map);
            $list = $this->pageshow('order',$data);
            $list = $this->protectData($list);//加密
        }
        $this->assign('form',$map);
        $this->assign('list',$list);
        $this->getSj();
        if ($this->hasRole('qudaozhuanyuan') || $this->hasRole('wangluozhuanyuan')){
            $this->assign('zhuanyuan',1);
        }else{
            $this->assign('zhuanyuan',0);
        }
        return $this->getOlistMoban();
    }

    public function add(){
        $order = new Order();
        if($this->request->isPost()){
            $data = $this->request->post();
            $re = $order->edit($data,$khd=1);//成功返回1
            return $re;
        }else{
            if( $this->hasRole(['qudaozhuguan','wangluozhuguan','yaoyuezhuguan','qudaozhuanyuan','wangluozhuanyuan','yaoyuezhuanyuan','huodongzhuguan','huodongzhuanyuan']) ){
                $this->assign('zgzy',1);//主管专员
            }else{
                $this->assign('zgzy',0);
            }
            $this->getSj();
            return $this->getAddMoban();
        }
    }

    public function edit(){
        $quanxian = $this->getQuanxian();
        $getqx = $quanxian['status'];
        if($getqx == 1){
            $order = new Order();
            $id = $this->request->param('id');
            if($this->request->isPost()){
                $data = $this->request->post();
                $re = $order->edit($data,$khd=1);//成功返回1
                return $re;
            }else{
                $map['id'] = $id;
                $info = $order->where($map)->find();
                $info = $this->protectData($info);//加密
                $this->assign('info',$info);//输出
                $this->getSj($info['hsdzid']);

                if( $this->hasRole(['qudaozhuguan','wangluozhuguan','yaoyuezhuguan','qudaozhuanyuan','wangluozhuanyuan','yaoyuezhuanyuan']) ){
                    $this->assign('zgzy',1);//主管专员
                }else{
                    $this->assign('zgzy',0);
                }
                if($this->hasRole('yaoyuezhuanyuan')){//邀约专员
                    $this->assign('yaozy',1);
                }else{
                    $this->assign('yaozy',0);
                }
                if($this->hasRole(['qudaozhuguan','qudaozhuanyuan'])){//渠道主管，专员不能修改医院信息
                    $this->assign('disyiyuan',1);
                }
                $this->assign('uid',session('admin_user.adminid'));

                $map = session('map');
                if($map['p'] == ''){
                    $this->assign('p',0);
                }else{
                    $this->assign('p',$map['p']);
                }

                // 页面模板
                return $this->getEditMoban();
            }
        }elseif($getqx == 2){
            $this->error("{$quanxian['msg']}");
        }else{
            $this->error('您没有权限！');
        }

    }

    //权限
    public function getQuanxian(){
        $order  = new Order();
        $quanxian = $order->getQuanxian();
        return $quanxian;
    }

    /**  查看订单详细
     * @return mixed
     */
    public function catorder(){
        $order = new Order();
        $id = $this->request->param('id');
        $map['id']     = $id;
        $info = $order->where($map)->find();
        $info = $this->protectData($info);//加密
        $this->assign('info',$info);//输出

        return $this->fetch();
    }

    /* 删除 */
    public function del()
    {
        $id   = $this->request->param('id');
        $quanxian = $this->getQuanxian();
        if($quanxian['status'] == 0){
            $this->error('您没有权限！');
        }else{
            $order = new order();
            $data['id']    = $id;
            $data['status'] = 0;   //逻辑删除  设为禁用
            $re = $order->isUpdate()->save($data);
            if($re){
                $order_log = new Order_log;
                $data['operation'] = 3;
                $ret = $order_log->log($data,1);//日志
                return ['status'=>1,'msg'=>"删除订单数据成功！"];
            }else{
                return ['status'=>0,'msg'=>"删除订单数据失败！"];
            }
        }
    }


    /* 查看日志  */
    public function catlog(){
        $order_log        = new Order_log();
        $map['order_id']  = $this->request->param('orderid');
        $info = $order_log->where($map)->order('id desc')->select();
        $this->assign('orderid',$map['order_id']);
        $this->assign('list',$info);
        return $this->fetch();
    }


    /**分配专员、分配活动
     * @return array|mixed
     */
    public function fenpei_dingdan(){
        $order = new Order();
        $data = $this->request->param();
        if($this->request->isPost()){
            return $order->fenpeiZy($data,1);
        }else{
            $data = $this->fenpei_Common_Data($data);
            $data['yykf'] = ['eq',0];//邀约客服为空
            if(!empty($data['ywstate'])){
                $data['ywstate'] = $data['ywstate'];
            }else{
                $data['ywstate'] = ['in',[1,4,7]];
            }
            $data['qd_depar'] = ['neq',3];//来电单不分配
            $data['id'] = ['notbetween',[15461,16020]];
            $list = $this->pageshow('order',$data);
            $this->assign('list',$list);
            return $this->fetch('fenpei-zy');
        }
    }

    public function fenpei_huisuo(){
        $order = new Order();
        $data = $this->request->param();
        if($this->request->isPost()){
            return $order->fenpeiHs($data,1);
        }else{
            $data = $this->fenpei_Common_Data($data);
            $data['hsdzid']  = ['eq',0];//会所门店为空
            $list = $this->pageshow('order',$data,'','','id desc',200);
            $this->assign('list',$list);
            return $this->fetch('fenpei-hd');
        }
    }

    public function fenpei_Common_Data(){
        $order = new Order();
        $data = $this->request->param();
        if(is_string($data['yiyuan'])){
            $data['yiyuan'] = explode(',',$data['yiyuan']);
        }
        if(empty($data['fpdata'])) {
            Session::delete('fpdata');
            session('fpdata',$data);
        }
        $data = session('fpdata');
        $this->assign('form',$data);
        if(!empty($data)){
            $data = $order->map($data);
        }
        $data['status'] = ['eq',1];
        $this->getFpSj($data['area']);
        return $data;
    }

    //获取PHONE
    public function getPhone()
    {
        $phone  = $this->request->param('phone');
        trim($phone);
        $id  = $this->request->param('id');
        if(!empty($phone)){
            $mendian =  Db::table('jtds_order')->where(['status'=>1,'maphone|otherphone'=>$phone])->find();
        }
        if(!empty($mendian) && $mendian['id'] != $id){
            return ['status'=>0,'msg'=>'电话号码重复！','icon'=>2];
        }else{
            return ['status'=>1,'msg'=>'恭喜您电话号码不重复','icon'=>1];
        }
    }
    //获取微信
    public function getWeixin()
    {
        $phone  = $this->request->param('weixin');
        trim($phone);
        $id  = $this->request->param('id');
        if(!empty($phone)){
            $mendian =  Db::table('jtds_order')->where(['status'=>1,'weixin'=>$phone])->find();
        }
        if(!empty($mendian) && $mendian['id'] != $id){
            return ['status'=>0,'msg'=>'微信号码重复！','icon'=>2];
        }else{
            return ['status'=>1,'msg'=>'恭喜您微信号码不重复！','icon'=>1];
        }
    }
    //获取QQ
    public function getQq()
    {
        $qq  = $this->request->param('qq');
        trim($qq);
        $id  = $this->request->param('id');
        if(!empty($qq)){
            $mendian =  Db::table('jtds_order')->where(['status'=>1,'qq'=>$qq])->find();
        }
        if(!empty($mendian) && $mendian['id'] != $id){
            return ['status'=>0,'msg'=>'QQ号码重复！','icon'=>2];
        }else{
            return ['status'=>1,'msg'=>'恭喜您电话号码不重复','icon'=>1];
        }
    }

    //微信推送
    public function pushweixin(){
        if($this->hasRole(['sheyingdiaodu','sheyingjingli'])){
            return ['msg'=>"您没有权限",'status'=>0,'icon'=>2,'url'=>url('olist')];
        }else{
            $order = new Order();
            $id = $this->request->param('id');
            $info = $order->where(['id'=>$id])->find();
//
//            if( !empty($info['fkry']) || $info['fkry']!=0 ){
//                return ['msg'=>"此订单已经反馈",'status'=>0,'icon'=>4,'url'=>url('olist')];
//            }else{

                if($this->request->isPost()){
                    $data = $this->request->param();
                    if( $this->hasRole(["qudaojingli","qudaozhuguan","qudaozhuanyuan","wangluojingli","wangluozhuguan","wangluozhuanyuan"]) ){
                        $data['qdbz'] = $data['bz'];
                    }elseif( $this->hasRole(["yaoyuejingli","yaoyuezhuguan","yaoyuezhuanyuan"]) ){
                        $data['yybz'] = $data['bz'];
                    }elseif (  $this->hasRole(["huodongjingli","huodongzhuguan","huodongzhuanyuan"]) ){
                        $data['hsbz'] = $data['bz'];
                    }else{
                        $data['qdbz'] = $data['bz'];
                    }
                    unset($data['bz']);
                    $olddata = $order->where('id', $data['id'])->find();

                    if($data['ywstate'] == 7 &&$this->hasRole(['qudaojingli','qudaozhuguan','qudaozhuanyuan','wangluojingli','wangluozhuguan','wangluozhuanyuan','huodongjingli','huodongzhuguan','huodongzhuanyuan']) ){
                       if($olddata['ywstate'] == 5 || $olddata['ywstate'] == 6){
                           return ['msg'=>"该客人已预约或拍摄，不能反馈",'status'=>1,'icon'=>4,'url'=>url('olist')];
                       }
                        $data['fkbm'] = session('admin_user.depar_id');//反馈部门
                        $data['fkry'] = session('admin_user.adminid');//反馈人员
                        $data['fktime'] = time();
                        if( $olddata['ywstate'] != 1 && $olddata['ywstate'] != 4 ){
                            $ywstate = $data['ywstate'];
                            unset($data['ywstate']);
                        }
                    }

//                    if( ($olddata['ywstate'] != 1 && $olddata['ywstate'] != 4 ) && $data['ywstate'] == 7  && $this->hasRole(['qudaojingli','qudaozhuguan','qudaozhuanyuan','wangluojingli','wangluozhuguan','wangluozhuanyuan','huodongjingli','huodongzhuguan','huodongzhuanyuan'])){
//                        $ywstate = $data['ywstate'];
//                        unset($data['ywstate']);
//                    }
                    $data = $order->editData($data,$olddata);

                    $order->allowField(true)->isUpdate()->save($data);
                    if( ($olddata['ywstate'] != 1 && $olddata['ywstate'] != 4) && $ywstate == 7 && $this->hasRole(['qudaojingli','qudaozhuguan','qudaozhuanyuan','wangluojingli','wangluozhuguan','wangluozhuanyuan','huodongjingli','huodongzhuguan','huodongzhuanyuan'])){
                        $data['ywstate'] =  $olddata['ywstate'];
                    }else{
                        $ywstate = $data['ywstate'];
                        //给转介绍人员发微信
                        $order->weixinToZjsry($data,$olddata);
                    }
                    $res = $order->weixin($data,$ywstate);
                    $order_log = new Order_log();
                    if($ywstate == 7){
                        $data['operation'] = 6;//意向反馈
                    }else{
                        $data['operation'] = 8;//邀约拍摄
                    }
                    $order_log->log($data,1);
                    return $res;
                }else{
                    if(is_administrator() ){
                        $this->assign('user',1); //管理员、总经理等
                    }elseif ($this->hasRole(['yaoyuejingli','yaoyuezhuguan','yaoyuezhuanyuan'])){
                        $this->assign('user',2);//邀约人员
                    }else{
                        $this->assign('user',3);//渠道、网络、会所
                    }
                    $this->getSj();
                    if( $this->hasRole(['qudaojingli','qudaozhuguan','qudaozhuanyuan','wangluojingli','wangluozhuguan','wangluozhuanyuan']) ){
                        $bz = $info['qdbz'];
                    }elseif( $this->hasRole(['yaoyuejingli','yaoyuezhuguan','yaoyuezhuanyuan']) ){
                        $bz = $info['yybz'];
                    }elseif( $this->hasRole(['huodongjingli','huodongzhuguan','huodongzhuanyuan']) ){
                        $bz = $info['hsbz'];
                    }
                    $this->assign('bz',$bz);//备注
                    $this->assign('info',$info);//输出
                    return $this->fetch();
                }

//            }




        }
    }

    public function sendmessage(){
        if($this->hasRole(['qudaojingli','qudaozhuguan','qudaozhuanyuan','wangluojingli','wangluozhuguan','wangluozhuanyuan','sheyingdiaodu'])){
            return ['msg'=>"您没有权限",'status'=>0,'icon'=>2,'url'=>url('olist')];
        }else{
            $order = new Order();
            $quanxian = $order->getQuanxian();
            if($quanxian['status'] == 0){
                return ['msg'=>"此状态不支持短信发送",'status'=>0,'icon'=>4,'url'=>url('olist')];
            }else{
                if( $this->request->isPost() ){
                    $data = $this->request->param();
                    $olddata = $order->where('id', $data['id'])->find();
                    $data = $order->doIncNum($data,$olddata);//处理自增和时间
                    $order->allowField(true)->isUpdate()->save($data);
                    $res = $order->message($data);
                    //给转介绍人员发微信
//                    $olddata = $order->where('id', $data['id'])->find();
                    $order->weixinToZjsry($data,$olddata);
                    if($res){
                        $order_log = new Order_log();
                        $data['operation'] = 7;//短信发送
                        $re = $order_log->log($data,1);
                        return ['msg'=>"发送成功",'status'=>1,'icon'=>1,'url'=>url('olist')];
                    }else{
                        return ['msg'=>"发送失败",'status'=>0,'icon'=>2,'url'=>url('sendmessage',['id'=>$data['id']])];
                    }
                }else{
                    $this->getSj();
                    $id = $this->request->param('id');
                    $info = $order->where(['id'=>$id,'status'=>1])->find();
                    $yycginfo = Db::name('group')->where(['id'=>$info['yycg'],'status'=>1])->find();
//                    $yaoyuephone = $order->getYykfID_Phone($info['yykf'])['yaoyuephone'];
                    $yaoyuephone = getYykfPhone($info['yykf']);
                    $this->assign('info',$info);
                    $this->assign('yycginfo',$yycginfo);
                    $this->assign('yaoyuephone',$yaoyuephone);

                    if( $this->hasRole(['yaoyuejingli','yaoyuezhuguan','yaoyuezhuanyuan']) ){
                        return $this->fetch('message-yaoyue');
                    } elseif( $this->hasRole(['huodongjingli','huodongzhuguan','huodongzhuanyuan']) ){
                        return $this->fetch('message-huisuo');
                    }else{
                        return $this->fetch('message');
                    }
                }
            }
        }
    }

    //动态获取区域
    public function getArea(){
        $city   = $this->request->param('city');
        if(!empty($city)){
            $area = Db::table('jtds_dq')->where(['status'=>1,'fid'=>$city])->field(['id','name'])->select();
        }else{
            $area = Db::table('jtds_dq')->where(['status'=>1,'fid'=>['neq',0]])->field(['id','name'])->select();
        }
        return $area;
    }
    //动态获取医院
    public function getyiyuan(){
        $area   = $this->request->param('area');
        if(empty($area)){
            $yiyuan = Db::table('jtds_leibie')->where(['status'=>1,'leibie'=>3])->field(['id','name'])->select();
        }else{
            $yiyuan = Db::table('jtds_leibie')->where(['status'=>1,'dq'=>$area,'leibie'=>3])->field(['id','name'])->select();
        }
        return $yiyuan;
    }
    //动态获取会所专员
    public function gethszy(){
        $hsmd  = $this->request->param('hsmd');
        if(empty($hsmd)){
            $hszy = Db::table('jtds_users')->where(['status'=>1,'depar_id'=>4])->field(['id','username'])->select();
        }else{
            $hszy = Db::table('jtds_users')->where(['status'=>1,'group_id'=>$hsmd,'depar_id'=>4])->field(['id','username'])->select();
        }
        return $hszy;
    }

    //动态获取摄影场馆信息
    public function getyycg(){
        $yycg   = $this->request->param('yycg');
        $yycginfo = Db::name('group')->where(['id'=>$yycg,'status'=>1])->find();
        $userinfo = DB::name('users')->where(['depar_id'=>5,'group_id'=>$yycg,'status'=>1])->find();
        $info = ['address'=>$yycginfo['address'],'tel'=>$yycginfo['tel'],'phone'=>$userinfo['phone']];
        return $info;
    }

    //根据部门动态获取人员
    public function getUser(){
        $bumen  = $this->request->param('bumen');
        if(!empty($bumen)){
            $user = Db::table('jtds_users')->where(['status'=>1,'depar_id'=>$bumen])->field(['id','username'])->select();
        }else{
            $user = Db::table('jtds_users')->where(['status'=>1])->field(['id','username'])->select();
        }
        return $user;
    }

    //选择显示字段
    public function selectziduan(){
//        $order = new Order();
//        $ids = $order->getYykfID_Phone()['id'];
//        $ids = getYykfPhone();
//        var_dump($ids);
//        return $this->fetch('selectziduan');
    }






}